﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;

namespace DesktopGarden.Classes
{
    public static class Helper
    {
        public static void Save(this Object obj, string fileName)
        {
            Stream stream = null;
            try
            {
                string objStream = JsonConvert.SerializeObject(obj, Formatting.Indented);
                File.WriteAllText(fileName, objStream);
            }
            catch
            {
                // do nothing, just ignore any possible errors
            }
            finally
            {
                if (null != stream)
                    stream.Close();
            }
        }

        public static T Load<T>(string fileName)
        {
            Stream stream = null;


            string s = File.ReadAllText(fileName);
            T obj = JsonConvert.DeserializeObject<T>(s);

            if (null != stream)
                stream.Close();


            return obj;
        } 
    }
}
